package naga.x.resource.image;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import naga.x.resource.image.support.ImageSupport;

@Controller
public class ImageController {
	
	@Autowired
	private ImageSupport support;

	@RequestMapping(path = "/icon/{id}", produces = MediaType.IMAGE_JPEG_VALUE, method = RequestMethod.GET)
//	@CrossOrigin(origins = { "*" }, allowedHeaders = { "*" }, methods = { RequestMethod.GET })
	@ResponseBody
	public byte[] getIcon(@PathVariable String id, HttpServletResponse res) {
//		add_header Access-Control-Allow-Origin *;
//        add_header Access-Control-Allow-Headers X-Requested-With;
//        add_header Access-Control-Allow-Methods GET,POST,OPTIONS
		res.addHeader("Access-Control-Allow-Origin", "*");
		byte[] data = support.find(id);
		return data;
	}
	
	@RequestMapping(path = "/icon/update", produces = MediaType.TEXT_PLAIN_VALUE, method = RequestMethod.PUT)
	@ResponseBody
	public String updateIcon(String channel, String url) {
		return support.update(channel, url);
	}
	
}
